package com.ljy.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ljy.constant.ShopType;
import com.ljy.dto.GoodsDTO;
import com.ljy.dto.GoodsPageDTO;
import com.ljy.entity.Goods;
import com.ljy.entity.ShopImage;
import com.ljy.entity.Specification;
import com.ljy.exception.DeletionNotAllowedException;
import com.ljy.mapper.GoodsMapper;
import com.ljy.mapper.ShopImageMapper;
import com.ljy.mapper.SpecMapper;
import com.ljy.result.PageResult;
import com.ljy.service.GoodsService;
import com.ljy.vo.GoodsVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class GoodsServiceImpl implements GoodsService {

    @Autowired
    GoodsMapper goodsMapper;
    @Autowired
    SpecMapper specMapper;
    @Autowired
    ShopImageMapper shopImageMapper;

    @Override
    @Transactional
    public void addGoods(GoodsDTO dto) {
        Goods goods= new Goods();
        BeanUtils.copyProperties(dto,goods);
        //TODO 改为自动补全日期
        goods.setCreateTime(LocalDateTime.now());
        goods.setUpdateTime(LocalDateTime.now());
        goods.setSalesVolume(0);
        //添加商品
        goodsMapper.insertGoods(goods);
        //获取添加完成的主键
        Integer goodsId=goods.getId();
        //插入spec规格表
        if(dto.getSpecs()!=null&&dto.getSpecs().size()!=0)
            specMapper.insertSpec(dto.getSpecs(),goodsId);
        //插入内容图片表设置图片type为商品
        List<ShopImage>shopImages=dto.getShopImages();
        if(shopImages!=null&&shopImages.size()!=0){
            shopImageMapper.insertImage(shopImages,goodsId,ShopType.Goods_Type);
        }
    }

    @Override
    public PageResult<GoodsVO> select(GoodsPageDTO dto) {
        PageHelper.startPage(dto.getPage(),dto.getPageSize());
        List<GoodsVO> list=goodsMapper.selectPage(dto);
        Page<GoodsVO>p= (Page<GoodsVO>) list;
        return new PageResult<>(p.getTotal(),p.getResult());
    }

    @Override
    public Goods selectById(Integer id) {
        Goods goods = goodsMapper.selectById(id);
        List<ShopImage> shopImages=shopImageMapper.selectByProductId(id,ShopType.Goods_Type);
        goods.setShopImages(shopImages);
        List<Specification>specs=specMapper.selectByGoodsId(id);
        goods.setSpecs(specs);
        return goods;
    }

    @Override
    @Transactional
    public void modifyGoods(GoodsDTO dto) {
        Integer goodsId= dto.getId();
        Goods goods=new Goods();
        BeanUtils.copyProperties(dto,goods);
        goods.setUpdateTime(LocalDateTime.now());
        goodsMapper.updateGoods(goods);
        //删除图片和规格
        shopImageMapper.deleteByProductId(goodsId,ShopType.Goods_Type);
        specMapper.deleteByGoodsId(goodsId);
        //再添加图片和规格
        if(dto.getSpecs()!=null&&dto.getSpecs().size()!=0)
            specMapper.insertSpec(dto.getSpecs(),goodsId);
        //插入内容图片表设置图片type为商品
        List<ShopImage>shopImages=dto.getShopImages();
        if(shopImages!=null&&shopImages.size()!=0){
            shopImageMapper.insertImage(shopImages,goodsId,ShopType.Goods_Type);
        }


    }

    @Override
    @Transactional
    public void batchDelete(List<Integer> ids)  {
        if(ids==null || ids.size()<1){
            throw new DeletionNotAllowedException("删除的商品不能为空");
        }
        goodsMapper.batchDelete(ids);
        shopImageMapper.batchDelete(ids,ShopType.Goods_Type);
        specMapper.batchDelete(ids);
    }

    @Override
    public void changeStatus(Short status, Integer id) {
        //TODO 如果货不够，不能启用
        goodsMapper.changeStatus(status,id);

    }
}
